import requestUrl from './requestUrl.js'

// 去除富文本的标签，只要内容
function strip_tags (content) {
  content = content.replace(/(\n)/g, "");
  content = content.replace(/(\t)/g, "");
  content = content.replace(/(\r)/g, "");
  content = content.replace(/<\/?[^>]*>/g, "");
  content = content.replace(/\s*/g, "");

  return content
}

// 时间戳转换成日期
function format_date(now) { 
	 var now = new Date(now * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
     var year=now.getFullYear(); 
     var month=now.getMonth()+1; 
     var day=now.getDate(); 
     var hour=now.getHours(); 
     var minute=now.getMinutes(); 
     var second=now.getSeconds(); 
     return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
} 

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const setSizeIndex = i => {
  i = i + 1
  return i;
}

// 重置时间
const reseTime = data => {
  const date = new Date(data * 1000);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ' ;
  return Y + M + D;
}

// 重置时间 + 时分秒
const reseTimeScond = data => {
  const date = new Date(data * 1000);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  const H = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const F = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  const S = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return Y + M + D + H + F + S;
}

// 把日期格式转成时间戳
function strtotime(str){
	var time = str.replace(/-/g,'/');
	var timestamp = new Date(time).getTime()/1000;
	return timestamp;
}

  /**
   * 从缓存获取用户信息
   */
function  get_user_cache_info() {
    let userInfo = uni.getStorageSync('gUserInfo') || null;
    if (userInfo == null) {
      return false;
    }
    return userInfo;
 }

// 请求封装
//param包括两部分 定义的请求头 header  和 参数 data
//
function request(url, way,callBack, param = {}) {
	// 获取token
	let user = get_user_cache_info();
	let token = (user == false) ? '' : user.token || '';
	url=url + token;

	// 定义默认的header  请求头
	var header={
		'content-type': 'application/x-www-form-urlencoded'
	}
	// 如果param参数里有请求头 header信息，那么就拼接 header 和param['header']
	if('header' in param){
		 Object.assign(header, param['header']);
	}
	
	// 取出请求参数
	var data=param['data'];
	uni.showLoading({
		title: '请稍后...',
		mask: false
	});
	uni.request({
		url: url,
		method: way,
		data: data,
		header: header,
		success: res => {
			callBack(res);
		},
		fail: () => {
			uni.showToast({
				title: '加载失败',
				image:'/static/icon/error.png',
				duration: 1500
			})
		},
		complete: () => {
			uni.hideLoading();
		}
	});
}

// 保存图片
function save_image(imgSrc){
	uni.showActionSheet({
		itemList: ['保存图片'],
		success: function(res) {
			// tapIndex==0 时，表示选中第一项
			if (res.tapIndex == 0) {
				uni.showLoading({
					title: '图片保存中'
				}),
				uni.downloadFile({
					url: imgSrc,
					success: function(result) {
						var temFilePath = result.tempFilePath;
						uni.saveImageToPhotosAlbum({
							filePath: temFilePath,
							success: function() {
								uni.showToast({
									title: "保存成功",
									duration: 1500
								})
							},
							complete: function() {
								uni.hideLoading();
							}
						})
					}
				})	
			}
		},
	})
}


// 获取图片真实url
function get_img_url(src){
	return requestUrl.host + '/public' + src;
}

// 处理性别
function get_sex(params){
	if(params == 2){
		return '男';
	}else if(params == 1){
		return '女';
	}else{
		return '保密'
	}
}

module.exports = {
	request,
	strip_tags,
	format_date,
	get_img_url,
	save_image,
	get_sex,
	strtotime
}
